package com.shure.surdes.modules.survey.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shure.surdes.common.query.condition.Condition;
import com.shure.surdes.common.result.R;
import com.shure.surdes.common.result.Result;
import com.shure.surdes.modules.survey.domain.Question;
import com.shure.surdes.modules.survey.query.QuestionQuery;
import com.shure.surdes.modules.survey.service.IQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 问卷题目Controller
 *
 * @author Shure
 * @date 2021-10-18
 */
@RestController
@RequestMapping("/survey/question")
public class QuestionController {
    @Autowired
    private IQuestionService questionService;

    /**
     * 查询问卷题目列表
     */
    @GetMapping("/list")
    public R list(@Validated QuestionQuery query) {
        LambdaQueryWrapper<Question> wrapper = Condition.getLambdaQueryWrapper(query);
        List<Question> list = questionService.list(wrapper);
        return Result.ok(list);
    }

    @GetMapping("/list/{surveyId}")
    public R list(@PathVariable("surveyId") Long surveyId) {
        List<Question> list = questionService.selectQuestionListBySurveyId(surveyId);
        return Result.ok(list);
    }


    /**
     * 获取问卷题目详细信息
     */
    @GetMapping(value = "/{questionId}")
    public R getInfo(@PathVariable("questionId") Long questionId) {
        return Result.ok(questionService.selectQuestionByQuestionId(questionId));
    }

    /**
     * 新增问卷题目
     */
    @PostMapping
    public R add(@RequestBody Question question) {
        return Result.ok(questionService.insertQuestion(question));
    }

    /**
     * 修改问卷题目
     */
    @PutMapping
    public R edit(@RequestBody Question question) {
        return Result.ok(questionService.updateQuestion(question));
    }

    /**
     * 删除问卷题目
     */
    @DeleteMapping("/{questionIds}")
    public R remove(@PathVariable Long[] questionIds) {
        return Result.ok(questionService.deleteQuestionByQuestionIds(questionIds));
    }

    /**
     * 更新问题序号和排序
     */
    @PutMapping("/updateQueNo")
    public R updateQueNo(@RequestBody List<Map<String, Object>> queNoes) {
        if (queNoes == null || queNoes.isEmpty()) {
            return Result.ok();
        }
        questionService.updateQuesiotnNo(queNoes);
        return Result.ok();
    }
}
